/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.clbs.ex.springgwt.client;

import com.clbs.ex.springgwt.client.model.Issue;
import com.clbs.ex.springgwt.client.service.IssueTrackerService;
import com.clbs.ex.springgwt.client.service.IssueTrackerServiceAsync;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import java.util.List;

/**
 *
 * @author peerawat.poombua
 */
public class MyGWTApplication implements EntryPoint {

    public IssueTrackerServiceAsync service = GWT.create(IssueTrackerService.class);

    private FlexTable issueEntries = new FlexTable();
    private VerticalPanel mainPanel = new VerticalPanel();
    private TextBox txtIssueName = new TextBox();
    private TextArea txtaIssueDescriptiong = new TextArea();
    private Button btnSave = new Button("Save");
    private Button btnCancel = new Button("Cancel");
    private FlexTable formLayout = new FlexTable();
    private DecoratorPanel formPanel = new DecoratorPanel();

    public void onModuleLoad() {

        formLayout.setCellSpacing(6);
        FlexCellFormatter cellFormatter = formLayout.getFlexCellFormatter();
        formLayout.setHTML(0, 0, "Issue Form");
        cellFormatter.setColSpan(0, 0, 2);
        cellFormatter.setHorizontalAlignment(
        0, 0, HasHorizontalAlignment.ALIGN_CENTER);
    
        formLayout.setHTML(1, 0, "Issue : ");
        formLayout.setWidget(1, 1, txtIssueName);
        formLayout.setHTML(2, 0, "Description : ");
        formLayout.setWidget(2, 1, txtaIssueDescriptiong);
        formLayout.setWidget(3, 0, btnSave);
        formLayout.setWidget(3, 1, btnCancel);
        formPanel.setWidget(formLayout);

        btnSave.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {   
                Issue issue = new Issue();
                issue.setName(txtIssueName.getText());
                issue.setDescription(txtaIssueDescriptiong.getText());
                issue.setIssue_statusID(1);
                addIssue(issue);
            }
        });

        btnCancel.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                txtIssueName.setText("");
                txtIssueName.setFocus(true);
                txtaIssueDescriptiong.setText("");
            }
        });


        mainPanel.add(issueEntries);
        mainPanel.add(formPanel);
        RootPanel.get("issuetracker").add(mainPanel);

        loadIssues();
    }

    private void loadIssues() {
        issueEntries.removeAllRows();
        issueEntries.setText(0, 0, "Status");
        issueEntries.setText(0, 1, "Issue Content");
        issueEntries.setText(0, 2, "Description");
        issueEntries.setText(0, 3, "Post");
        issueEntries.getRowFormatter().setStyleName(0, "issueHeader");
        issueEntries.setStyleName("issueList");
        issueEntries.getCellFormatter().setStyleName(0, 0, "issueListButtonColumn");
        issueEntries.getCellFormatter().setStyleName(0, 3, "issueListButtonColumn");

        service.getAllIssues(new AsyncCallback<List<Issue>>() {

            public void onFailure(Throwable caught) {
                GWT.log("GetAllIssues fail");
            }

            public void onSuccess(List<Issue> issues) {
                int row = 0;
                for (int index = 0; index < issues.size(); index++) {
                    row = issueEntries.getRowCount();
                    issueEntries.setText(row, 0, issues.get(index).getIssueStatus().getName());
                    issueEntries.getCellFormatter().setStyleName(row, 0, "issueListButtonColumn");
                    issueEntries.setText(row, 1, issues.get(index).getName());
                    issueEntries.setText(row, 2, issues.get(index).getDescription());

                    Button btnPost = new Button("Post");
                    btnPost.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            Widget source = (Widget) event.getSource();
                            int left = source.getAbsoluteLeft() + 10;
                            int top = source.getAbsoluteTop() + 10;
                            
                            DecoratedPopupPanel popup = new DecoratedPopupPanel();
                            popup.setAutoHideEnabled(true);
                            popup.setWidget(new Label("Do something."));
                            popup.setWidth("100");
                            popup.setPopupPosition(left, top);
                            popup.show();
                        }
                    });

                    issueEntries.setWidget(row, 3, btnPost);
                    issueEntries.getCellFormatter().setStyleName(row, 3, "issueListButtonColumn");
                }
            }
        });

    }

    private void addIssue(Issue issue) {
        
        service.createIssue(issue, new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                GWT.log("Add Issue fail");
            }

            public void onSuccess(Void result) {
                loadIssues();
            }
        });

    }

}
